//
//  ProductHeaderView.m
//  LearnObjectiveC
//
//  Created by 吴可高 on 16/6/14.
//  Copyright © 2016年 吴可高. All rights reserved.

#import "ProductHeaderView.h"
#import "UtilityTool.h"

@interface ProductHeaderView()
{
    UIImageView * _bannerImgView;
    UILabel * _productNameLabel;
    UILabel * _descProduceLabel;
    UILabel *_priceLabel;
    UILabel *_reduceLabel;
    UILabel *_totatLabel;
    
}
@end
@implementation ProductHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setUp];
    }
    return self;
}

-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
}
#pragma mark Initialize View Style
-(void)setUp
{
    self.backgroundColor = KColor(240, 240, 240);
    UIImageView * bannerImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    [self addSubview:bannerImgView];

    [bannerImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0.f);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(kScreen_Width);
        make.height.mas_equalTo(200);
    }];
    bannerImgView.layer.masksToBounds = YES;
    bannerImgView.layer.cornerRadius = 7.f;
    bannerImgView.backgroundColor = [UIColor lightGrayColor];
  //  [self initBaseInfoMsgView];
    
    [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(headerAttributeChanged) userInfo:nil repeats:YES];
}
-(void)initBaseInfoMsgView
{
    UIImageView * bannerImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    [bannerImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0.f);
        make.top.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width - 10, 100));
    }];
    bannerImgView.layer.masksToBounds = YES;
    bannerImgView.layer.cornerRadius = 7.f;
    bannerImgView.backgroundColor = [UIColor lightGrayColor];
   // [self addSubview:bannerImgView];
    _bannerImgView  = bannerImgView;
    //
    UILabel * productNameLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    productNameLabel.numberOfLines = 0.f;
    productNameLabel.text = @"百草味 - 葡萄干组合套餐600g,无核葡萄干＋玫瑰红葡萄干＋金皇后葡萄干";
    productNameLabel.textColor = [UIColor grayColor];
    [productNameLabel sizeToFit];
    [productNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(bannerImgView.mas_left).offset(0);
        make.top.mas_equalTo(bannerImgView.mas_bottom).offset(10);
        make.right.mas_greaterThanOrEqualTo(self.mas_right).offset(20);
    }];
   // [self addSubview:productNameLabel];
    _productNameLabel = productNameLabel;
    //
    UILabel * descProduceLabel = [[UILabel alloc]initWithFrame:CGRectZero];
     descProduceLabel.text = @"[618零食狂欢扒］,满138送豆腐干100g，领券88－6 送 168-10领券88－6 送 168-10领券88－6 送 168-10领券88－6 送 168-10";
     descProduceLabel.textColor = [UIColor redColor];
     descProduceLabel.numberOfLines = 5.f;
    [descProduceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(productNameLabel.mas_left).offset(0);
        make.top.mas_equalTo(productNameLabel.mas_bottom).offset(10);
        make.right.mas_greaterThanOrEqualTo(self.mas_right).offset(20);
    }];
    //[self addSubview:descProduceLabel];
    _descProduceLabel = descProduceLabel;
    //
    UILabel * priceLabel = [UILabel new];
    priceLabel.textColor = [UIColor redColor];
    priceLabel.text = @"¥ 100.00";
    
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(0);
        make.top.mas_equalTo(_descProduceLabel.mas_bottom).offset(25);
        
    }];
    //[self addSubview:priceLabel];
    _priceLabel = priceLabel;
    //
    UILabel * reduceLabel = [UILabel new];
    reduceLabel.text = @"降价通知";
    reduceLabel.textColor = [UIColor redColor];
    [reduceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(priceLabel.mas_centerY).offset(1.f);
        make.right.mas_equalTo(self.mas_right).offset(30.f);
    }];
   // [self addSubview:reduceLabel];
    _reduceLabel = reduceLabel;
    //
    UILabel * totatLabel = [UILabel new];
    totatLabel.textColor = [UIColor redColor];
    totatLabel.text = @"[618手机专享] 比pc端省7元";
    [totatLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(priceLabel.mas_left).offset(1.f);
        make.top.mas_equalTo(priceLabel.mas_bottom).offset(20.f);
        make.right.mas_greaterThanOrEqualTo(self.mas_right).offset(30.f);
    }];
   // [self addSubview:totatLabel];
    _totatLabel = totatLabel;
    // 白条
    UIView * lineWhiteBarView = [UIView new];
    lineWhiteBarView.backgroundColor = [UIColor lightGrayColor];
    //[self addSubview:lineWhiteBarView];
    
    UILabel * whiteReaderLabel = [UILabel new];
    whiteReaderLabel.textColor = KColor(234, 234, 234);
    whiteReaderLabel.text =@"白条";
    [self addSubview:whiteReaderLabel];
    
    UILabel * totastWhiteLabel = [UILabel new];
    totastWhiteLabel.text = @"打白条享6期免息/12期信息费五折";
    totastWhiteLabel.backgroundColor = [UIColor blackColor];
    [self addSubview:totastWhiteLabel];
    // 领券
    UIView * cashCouponView   = [[UIView alloc]init];
    cashCouponView.backgroundColor  = [UIColor whiteColor];
   // [self addSubview:cashCouponView];
    //
    UIView * cashLineView = [UIView new];
    cashLineView.backgroundColor  = [UIColor grayColor];
    [cashCouponView addSubview:cashLineView];
    
    UILabel * cashLabel = [UILabel new];
    cashLabel.text = @"领券";
    cashLabel.textColor = KColor(234, 234, 234);
    [cashCouponView addSubview:cashLabel];
    UILabel * cashTypeLabel = [UILabel new];
    cashTypeLabel.textColor = [UIColor redColor];
    cashTypeLabel.text = @"满178减10  满98减5   满88减4";
    [cashCouponView addSubview:cashTypeLabel];
    //保障服务
    UIView * safeguardView = [UIView new];
    safeguardView.backgroundColor = [UIColor whiteColor];
   // [self addSubview:safeguardView];
    
    UILabel * safeguardThemeLabel = [UILabel new];
    safeguardThemeLabel.text = @"已选择";
    safeguardThemeLabel.textColor = [UIColor lightGrayColor];
    [safeguardView addSubview:safeguardThemeLabel];
  
    UILabel * safeguradTypeLabel = [[UILabel alloc]init];
    safeguradTypeLabel.textColor = [UIColor blueColor];
    safeguradTypeLabel.text = @"2个,保障服务信息";
    [safeguardView addSubview:safeguradTypeLabel];
    //地址信息
    UIView * addressInfoView  = [[UIView alloc]init];
    addressInfoView.backgroundColor  = [UIColor whiteColor];
    //[self addSubview:addressInfoView];

    UILabel * detailAddressTypeLabel =[UILabel new];
    detailAddressTypeLabel.textColor  = [UIColor lightGrayColor];
    detailAddressTypeLabel.text = @"送至";
    [addressInfoView addSubview:detailAddressTypeLabel];
    UILabel * detailAddressLabel     =[UILabel new];
    detailAddressLabel.textColor = [UIColor blueColor];
    detailAddressLabel.text  = @"湖北省武汉市洪山区光谷软件园光谷E城 \n\n 现货";
    [addressInfoView addSubview:detailAddressLabel];
    
    UILabel *expensesTypeLabel = [UILabel new];
    expensesTypeLabel.text= @"运费";
    expensesTypeLabel.textColor = [UIColor lightGrayColor];
    [addressInfoView addSubview:expensesTypeLabel];
    
    UILabel * expensesDescLabel = [[UILabel alloc]init];
    expensesDescLabel.text =  @"店铺单笔订单不满68元,在线支付5元费用";
    expensesDescLabel.textColor = [UIColor lightGrayColor];
    [addressInfoView addSubview:expensesDescLabel];
    
    NSArray * remarks = @[@"店铺发货&售后",@"七天退货"];
    for (int i = 0 ; i < remarks.count ; i ++) {
        UIButton * remakItem = [UIButton buttonWithType:UIButtonTypeCustom];
        [remakItem setTitle:remarks[i] forState:UIControlStateNormal];
         remakItem.enabled = NO;
         UIImage * drawImg = [UtilityTool createImageByColor:[UIColor cyanColor] rect:CGRectMake(0, 0, 12, 12)];
        [remakItem setImage:drawImg forState:UIControlStateNormal];
        [remakItem.titleLabel sizeToFit];
        [addressInfoView addSubview:remakItem];
    }
}
#pragma mark View Algorithm Processor

#pragma mark Setter Method View Style

-(CGFloat)productHeaderHeight
{
    return _productHeaderHeight;
}
#pragma mark Getter Method View Style

#pragma mark IBOutlet Action View
-(void)headerAttributeChanged
{
    UIView * lastObject = [self.subviews lastObject];
    
    NSInteger  value = arc4random()%300;
    if (value > 210) {
        value  = value;
    }
    else
        value = 210;
    [lastObject mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(value);
    }];
}
#pragma mark Possess  Delegate View（委托代理者）
#pragma mark Other Method
-(void)layoutSubviews
{
    [super layoutSubviews];
     UIView * lastObject = [self.subviews lastObject];
     self.productHeaderHeight =  lastObject.getBottom + 8;

}
@end
